package com.minimoment.model.action;

import com.minimoment.base.BaseAction;
import com.minimoment.model.domain.Movie;
import com.opensymphony.xwork2.ActionContext;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * Created by etund on 2014/11/1.
 */
@Controller
@Scope("prototype")
public class MovieAction extends BaseAction<Movie> {
    private String result;
    private String theaterId;
    private  JSONArray resultTree;
    /**
     * 查询选定影片的详细信息
     * @return
     * @throws Exception
     */
    public String moviePreview() throws Exception{
        Movie movie = this.movieService.getById(model.getId());
        ActionContext.getContext().getValueStack().push(movie);
        return "moviePreview";
    }

    /**
     * 根据传过来的TheaterId返回所在影院正在播放的电影
     * @return
     * @throws Exception
     */
    public String getMovieByTheaterId() throws  Exception{
        if(theaterId!=null&&!theaterId.equals("")){
            this.movies = this.movieService.getByTheaterId(theaterId);
            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.setExcludes(new String[]{"screeningses","movieNewses","users","umComemts","theaters","telecines"});
            movies = this.movieService.getByFlag(1L,2L,null);
            resultTree = JSONArray.fromObject(movies,jsonConfig);
            result = resultTree.toString();
        }
        return "getMovieByTheaterId";
    }

    /**
     * android返回在热播的电影
     * @return
     * @throws Exception
     */
    public String androidCurrentMovies() throws Exception{
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[]{"screeningses","movieNewses","users","umComemts","theaters","telecines"});
        movies = this.movieService.getByFlag(2L,1L,null);
        resultTree = JSONArray.fromObject(movies,jsonConfig);
        result = resultTree.toString();
        System.out.println(result+"=================json=================");
        return "androidCurrentMovies";
    }

    /**
     * android返回在即将上映的电影
     * @return
     * @throws Exception
     */
    public String androidPreviewMovies() throws Exception{
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[]{"screeningses","movieNewses","users","umComemts","theaters","telecines"});
        movies = this.movieService.getByFlag(3L, 3L, null);
        System.out.println(movies.size());
        resultTree = JSONArray.fromObject(movies,jsonConfig);
        result = resultTree.toString();
        return "androidPreviewMovies";
    }
    /**
     * 查询影片为首页显示
     * @return
     * @throws Exception
     */
    public String other() throws Exception{
        return null;
    }


    public JSONArray getResultTree() {
        return resultTree;
    }

    public void setResultTree(JSONArray resultTree) {
        this.resultTree = resultTree;
    }


    public String theaterByMovie() throws  Exception {
        return null;
    }

    public String getTheaterId() {
        return theaterId;
    }

    public void setTheaterId(String theaterId) {
        this.theaterId = theaterId;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }
}
